using System.Collections.Generic;
using System.Xml.Linq;
using TDUnit.Common.TestData;

namespace TDUnit.TestDataParsers.XmlParser
{
    public class ColumnElementParser : ElementParserBase
    {
        public void Parse(IEnumerable<XElement> colElements, Row row)
        {
            foreach(var colElement in colElements)
            {
                ParseColumnElement(colElement, row);
            }
        }

        private void ParseColumnElement(XElement colElement, Row row)
        {
            var colName = colElement.Attribute("Name");
            if (colName == null)
                throw new XmlTestDataParserException("The Name attribute must be set on Column elements.", colElement);

            var colValue = string.Empty;
            var colValueAttrib = colElement.Attribute("Value");
            if (colValueAttrib != null)
                colValue = colValueAttrib.Value;

            var shouldQuoteAttrib = colElement.Attribute("ShouldQuote");
            bool? shouldQuote = null;
            if (shouldQuoteAttrib != null)
                shouldQuote = ParseAttributeAsBoolean(shouldQuoteAttrib, "ShouldQuote");

            row.AddColumn(colName.Value, colValue, shouldQuote);
        }
    }
}